<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem  v-if="category.top" :to="`/cate/${category.top.id}`">{{ category.top.name }}</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
        <XtxBreadItem :key="category.sub.id"  v-if="category.sub">{{ category.sub.name }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed } from 'vue'

export default {
  name: 'SubBread',
  setup() {
    const store = useStore()
    const route = useRoute()
    // 通过计算属性得到，二级类目的名称和ID，一级类目的名称和ID {top:{name:'居家',id:10050},sub:{name:'厨具',id:100089}}
    const category = computed(() => {
      // 二级分类的id
      const id = route.params.id
      // 定义空对象保存处理的结果
      const obj = {}
      store.state.cate.cateList.forEach(top => {
        top.children && top.children.forEach(sub => {
          if (sub.id === id) {
            obj.top = { id: top.id, name: top.name }
            obj.sub = { id, name: sub.name }
          }
        })
      })
      // 只有计算的属性的结果在模板中使用，才能输出结果
      // console.log(obj)
      return obj
    })
    return { category }
  }
}
</script>
